package io.renren.com.qywt.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.com.qywt.dao.CarriageFeeDao;
import io.renren.com.qywt.dto.*;
import io.renren.com.qywt.entity.AftersaleCarriageFeeEntity;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.enums.DeleteEnum;
import io.renren.commons.tools.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TaskCarriageFeeService extends BaseServiceImpl<CarriageFeeDao, CarriageInfoDTO> {
    @Autowired
    private AftersaleCarriageFeeService carriageFeeService;
    @Autowired
    private AftersaleFinanceCollectService aftersaleFinanceCollectService;
    @Autowired
    private TaskContractService contractService;

    @DataSource("slave1")
    @Transactional
    public List<AftersaleCarriageFeeEntity> getCarriageInfoList(UserDetail userDetail,AftersaleFinanceCollectDTO dto){
        Map<String,Object> params=new HashMap<>();
        Date collectDate=new Date();
        if(ObjectUtil.isNull(dto)){
            collectDate=DateUtils.addDateDays(new Date(),-1);
        }else{
            collectDate=dto.getCollectDate();
        }
        String collectDateStr=DateUtils.format(collectDate,DateUtils.DATE_PATTERN);
        params.put("collectDate",collectDateStr);
        List<CarriageInfoDTO> carriageInfoList=baseDao.getCarriageInfoList(params);
        List<AftersaleCarriageFeeEntity> aftersaleCarriageFeeEntityList=new ArrayList<>();

        /**
         * 临时变量
         */
        Map<String,Object> map=new HashMap<>();
        double weightFee=0.0;
        double numFee=0.0;
        double cubeFee=0.0;

        for(CarriageInfoDTO carriageInfoDTO:carriageInfoList){
            ContractDTO contractDTO = contractService.getContractBySkuId(carriageInfoDTO.getSkuId());
            if(ObjectUtil.isNotNull(contractDTO)){
                if(contractDTO.getNeedMax()==1){//需要修正值
                    MaxContractDTO maxContractDTO = contractService.getMaxContractBySkuId(carriageInfoDTO.getSkuId());
                    if(ObjectUtil.isNotNull(maxContractDTO)){
                        weightFee=carriageInfoDTO.getShippedQty()*carriageInfoDTO.getSkuWeight()*maxContractDTO.getCarrierWeight();
                        numFee=carriageInfoDTO.getShippedQty()*maxContractDTO.getCarrierNum();
                        cubeFee=carriageInfoDTO.getShippedQty()*(carriageInfoDTO.getSkuCube()/1000000)*maxContractDTO.getCarrierCube();
                        double max=Collections.max(Arrays.asList(weightFee,numFee,cubeFee));
                        if(max==weightFee){//按照重量计算
                            carriageInfoDTO.setRecordType(2);
                        }else if(max==numFee){//按照数量收费
                            carriageInfoDTO.setRecordType(1);
                        }else if(max==cubeFee){
                            carriageInfoDTO.setRecordType(3);
                        }
                        carriageInfoDTO.setMaxCost(max);
                    }
                }
                map.put("contractId",contractDTO.getContractId());
                map.put("supplierId",carriageInfoDTO.getCarrierId());
                map.put("provinceId",carriageInfoDTO.getStateId());
                map.put("weight",carriageInfoDTO.getSkuWeight());
                SupplierCarrierFeeDTO supplierCarrierFeeDTO=contractService.getContractCarrierFees(map);
                if(ObjectUtil.isNotNull(supplierCarrierFeeDTO)){
                    //判断是否超过首重
                    if(carriageInfoDTO.getSkuWeight()>supplierCarrierFeeDTO.getFirstWeight()){
                        double firstWeightFee=supplierCarrierFeeDTO.getFirstWeightFee().doubleValue();
                        double overWeightPrice=supplierCarrierFeeDTO.getOverWeightPrice().doubleValue();
                        double carrier=carriageInfoDTO.getShippedQty()*(firstWeightFee+(carriageInfoDTO.getSkuWeight()-firstWeightFee)*overWeightPrice);
                        carriageInfoDTO.setTotalCost(carrier);
                    }else{
                        double carrierFee=supplierCarrierFeeDTO.getSegmentWeightFee().doubleValue();
                        double carrier = carriageInfoDTO.getShippedQty()* carrierFee;
                        carriageInfoDTO.setTotalCost(carrier);
                    }
                }
            }
            //入库费明细
            AftersaleCarriageFeeEntity entity= new AftersaleCarriageFeeEntity();
            entity.setBatchNo(collectDateStr.replaceAll("-",""));
            entity.setEjectNo(carriageInfoDTO.getShipmentKey());
            entity.setEjectDate(carriageInfoDTO.getHandoverTime());
            entity.setReferenceNo(carriageInfoDTO.getReferNo());
            entity.setDeliveryCompany(carriageInfoDTO.getShortName());
            entity.setProdDetail(carriageInfoDTO.getItemName());
            entity.setAddress(carriageInfoDTO.getAddress());
            entity.setProdNum(carriageInfoDTO.getShippedQty());
            entity.setProdWeight(carriageInfoDTO.getSkuWeight());
            entity.setProdVolume(carriageInfoDTO.getSkuCube());
            entity.setCarriageFee(BigDecimal.valueOf(carriageInfoDTO.getTotalCost()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            entity.setStatus(1);
            entity.setDelFlag(DeleteEnum.NO.value());
            entity.setTenantCode(userDetail.getTenantCode());
            entity.setCreator(userDetail.getId());
            aftersaleCarriageFeeEntityList.add(entity);
        }
        return aftersaleCarriageFeeEntityList;
    }

}
